package eight.test_8_7;

import java.util.Scanner;

public class TestMain4 {
    public static boolean[][] booleans;
    public static int[] arrx = {0,0,-1,1};
    public static int[] arry = {1,-1,0,0};
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m = scanner.nextInt();
        int xs = scanner.nextInt() - 1;
        int ys = scanner.nextInt() - 1;
        int xt = scanner.nextInt() - 1;
        int yt = scanner.nextInt() - 1;
        scanner.nextLine();
        char[][] arr = new char[n][m];
        booleans = new boolean[n][m];
        for (int i = 0; i < n; i++) {
            String str = scanner.nextLine();
            char[] chars = str.toCharArray();
            arr[i] = chars;
        }
        int min = Integer.MAX_VALUE;
        boolean b = true;
        if (arr[xs][ys] == '*' || arr[xt][yt] == '*') {
            System.out.println(-1);
            return;
        }
        for (int i = 0; i < 4; i++) {
            int x = xs + arrx[i];
            int y = ys + arry[i];
            if(x < 0 || y >= arr[0].length || x >= arr.length || y < 0) {
                continue;
            }
            if(arr[x][y] == '*' || booleans[x][y]) {
                continue;
            }
            booleans[xs][ys] = true;
            int tmp = dfs(x,y,xt,yt,arr);
            if(tmp != -1) {
                b = false;
                min = Math.min(min,tmp);
            }
            booleans[xs][ys] = false;
        }
        if(b) {
            System.out.println(-1);
        } else {
            System.out.println(min);
        }
    }
    private static int dfs(int xs,int ys,int xt,int yt,char[][] arr) {
        if(xs == xt && ys == yt) {
            return 1;
        }
        if(arr[xs][ys] == '*') {
            return -1;
        }
        boolean b = true;
        int min = Integer.MAX_VALUE;
        for (int i = 0; i < 4; i++) {
            int x = xs + arrx[i];
            int y = ys + arry[i];
            if(x < 0 || y >= arr[0].length || x >= arr.length || y < 0) {
                continue;
            }
            if(arr[x][y] == '*' || booleans[x][y]) {
                continue;
            }
            booleans[xs][ys] = true;
            int tmp = dfs(x,y,xt,yt,arr);
            if(tmp != -1) {
                b = false;
                min = Math.min(min,tmp);
            }
            booleans[xs][ys] = false;
        }
        if(b) {
            return -1;
        } else {
            return min + 1;
        }
    }
}
